#!/bin/bash
#File:set_es_run.sh
es_setup_dir="/opt/elasticsearch-5.5.0"
userName="es_admin"

# 添加并授权ES的使用给新用户
egrep "^$userName" /etc/passwd >& /dev/null  
if [ $? -ne 0 ]; then  
    echo "开始创建新用户($userName)完成"
    useradd $userName 
    echo "123456" |passwd --stdin $userName 
    chown -R $userName "$es_setup_dir" 
    chgrp -R $userName "$es_setup_dir"
    echo "1.添加新用户($userName)(123456),并授权ES文件"
else
    chown -R $userName "$es_setup_dir"
    chgrp -R $userName "$es_setup_dir"
    echo "1.已存在es_admin用户,授权ES文件"
fi  


# 设置用户能打开的最大文件数(65536)
sed -i -e '/$userName/d' /etc/security/limits.conf   # 删除包含es_admin字符串的行
echo -e "--------------------- ES 运行配置($userName) ---------------------" >> /etc/security/limits.conf
echo -e "$userName soft nofile 65536" >> /etc/security/limits.conf  #用户最大打开文件数
echo -e "$userName hard nofile 65536" >> /etc/security/limits.conf
echo -e "$userName soft nproc 10000" >> /etc/security/limits.conf   #用户最大进程打开数
echo -e "$userName hard nproc 10000" >> /etc/security/limits.conf
echo "2.设置用户能打开的最大文件数(65536)完成"

# 设置系统最大系统内存参数(vm.max_map_count=262144) 
sed -i -e '/vm.max_map_count/d' /etc/sysctl.conf  # 删除包含vm.max_map_count字符串的行
echo -e "--------------------- ES 运行配置(vm.max_map_count) ---------------------"  >> /etc/sysctl.conf
echo -e "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p >/dev/null 2>&1 # 执行命令后不管是命令输出结果或是异常都不打印任何信息
echo "3.配置vm.max_map_count=262144完成"
 

# 关闭防火墙
chkconfig iptables off  > /dev/null
service iptables stop > /dev/null
echo "4.关闭防火墙操作完成"

sed -i -e '/set_es_run_sh/d' "$es_setup_dir/config/elasticsearch.yml"  
echo -e "--------------------- change_(set_es_run_sh) ---------------------" >> "$es_setup_dir/config/elasticsearch.yml"
# linxu内核版本不支持SecComp，修改elasticsearch配置文件不进行自动检测
echo -e "bootstrap.memory_lock: false #set_es_run_sh" >> "$es_setup_dir/config/elasticsearch.yml"
echo -e "bootstrap.system_call_filter: false #set_es_run_sh" >> "$es_setup_dir/config/elasticsearch.yml"
echo "5.设置elasticsearch配置文件,不进行自动检测SecComp完成"
# 设置network.host: 0.0.0.0
echo -e "network.host: 0.0.0.0 #set_es_run_sh" >> "$es_setup_dir/config/elasticsearch.yml"
echo "6.设置network.host: 0.0.0.0完成"

echo  -e "\n您可以切换为es_admin用户开始操作ES启动！"